<?php

namespace app\api\controller;

use app\api\model\User as UserModel;
use app\api\service\Payment as PaymentService;
use app\common\enum\OrderType as OrderTypeEnum;
use app\common\model\Joins;
use app\common\service\Order as OrderService;
use app\store\model\Setting as SettingModel;

/**
 * 用户管理
 * Class User
 * @package app\api
 */
class User extends Controller
{
    /**
     * 用户自动登录
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function login()
    {
        $model = new UserModel;
        return $this->renderSuccess([
            'user_id' => $model->login($this->request->post()),
            'token' => $model->getToken()
        ]);
    }

    /**
     * 当前用户详情
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 当前用户信息
        $userInfo = $this->getUser();
        return $this->renderSuccess(compact('userInfo'));
    }

    /**
     * 加盟提交
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function submitOrder()
    {
        $user = $this->getUser();
        $address = $this->request->post('address');
        if (empty($address)) {
            return $this->renderError('地址不能为空');
        }
        $areaId = $this->request->post('area_id');
        if (empty($areaId)) {
            return $this->renderError('区标识不能为空');
        }
        $name = $this->request->post('name');
        if (empty($name)) {
            return $this->renderError('姓名不能为空');
        }
        $idcard = $this->request->post('idcard');
        /*if (empty($idcard)) {
            return $this->renderError('身份证号不能为空');
        }*/
        $idcard_file1 = $this->request->post('idcard_file1');
        /*if (empty($idcard_file1)) {
            return $this->renderError('身份证正面不能为空');
        }*/

        $idcard_file2 = $this->request->post('idcard_file2');
        /*if (empty($idcard_file2)) {
            return $this->renderError('身份证反面不能为空');
        }*/
        $phone = $this->request->post('phone');
        if (empty($phone)) {
            return $this->renderError('手机号不能为空');
        }
        $age = $this->request->post('age');
        if (empty($age)) {
            return $this->renderError('年龄不能为空');
        }
        $Brand = $this->request->post('Brand');
        if (empty($Brand)) {
            return $this->renderError('品牌不能为空');
        }

        $join = Joins::where([
            'user_id' => $user->user_id,
            'area_id' => $areaId,
        ])->find();


        $price = SettingModel::getItem('store')['join_price'] ?? 0;
        if (empty($join)) {
            $join = Joins::create([
                'Brand' => $Brand,
                'user_id' => $user->user_id,
                'area_id' => $areaId,
                'address' => $address,
                'name' => $name,
                'idcard' => !empty($idcard)??'',
                'idcard_file1' => !empty($idcard_file1)??'',
                'idcard_file2' => !empty($idcard_file2)??'',
                'phone' => $phone,
                'age' => $age,
                'price' => $price,
                'is_pay'=>0,
                'wxapp_id' => 10003,
                'order_num' => OrderService::createOrderNo(),
                'wechat_order_num' => OrderService::createOrderNo(),
            ]);
        }

        $payment = PaymentService::wechat(
            $user,
            null,
            $join['wechat_order_num'],
            $join['price'],
            OrderTypeEnum::JOIN,
            base_url() . 'join_notice.php'
        );

        return $this->renderSuccess(compact('payment'));
    }
}
